<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="line" />

      <el-card class="tree-card">
        <TreeTools :data="company" :is-root="true" @addDepts="addDepts" @delDepts="loadPage" />
        <hr>
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <TreeTools slot-scope="scope" :data="scope.data" @addDepts="addDepts" @delDepts="loadPage" @editDepts="editDepts" />
        </el-tree>
      </el-card>
      <AddDept ref="editDepts" :show-dialog.sync="showDialog" :data="node" @addDepts="loadPage" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { convertTreeData } from '@/utils/index'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      node: {},
      showDialog: false,
      // 本来组织结构的数据应该从服务器获取,
      // 但是现在先不管, 写死数据作为测试
      departs: [

      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {

      }
    }
  },
  created() {
    this.loadPage()
  },
  methods: {

    loadPage() {
      getDepartments().then(res => {
        console.log(res)
        this.company = {
          name: res.companyName,
          manager: '负责人',
          id: ''
        }
        this.departs = convertTreeData(res.depts, '')
      })
    },
    addDepts(node) {
      // 显示添加对话框
      this.showDialog = true
      // 父组件接收子组件传来的部门数据
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      // 调用子组件的方法
      this.$nextTick(() => {
        this.$refs.editDepts.getCompanyDepartment(node.id)
      })
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
